<template>
  <div class="visitor-container">
    <div class="flex-row justify-between">
      <!-- search -->
      <div class="searchBox">
        <el-form inline :model="queryInfo" class="demo-form-inline" label-position="right" label-width="80px">
          <el-form-item label="车间编号">
            <el-input v-model="queryBase.numbering" style="width: 200px;" placeholder="请输入车间编号" />
          </el-form-item>
           <el-form-item label="车间名称">
            <el-input v-model="queryBase.name" style="width: 200px;" placeholder="请输入车间名称" />
          </el-form-item>
          <el-form-item label="产线编号">
            <el-input v-model="queryInfo.productionLineNumber" style="width: 200px;" placeholder="请输入产线编号" />
          </el-form-item>
          <el-form-item label="产线名称">
            <el-input v-model="queryInfo.productionLineName" style="width: 200px;" placeholder="请输入产线名称" />
          </el-form-item>
        </el-form>
      </div>
      <div class="mg-top-4" style="flex-shrink: 0;">
        <el-button type="info" size="mini" @click="handleQuery">查询</el-button>
        <el-button size="mini" @click="reset">重置</el-button>
      </div>
    </div>
    <add-button title="新增" @addClick="handleAdd" size="mini" />
    <el-table
      v-loading="listLoading"
      :data="tableData" border
      header-cell-class-name="table-header-cell"
      header-row-class-name="table-header-row"
      class="table-container" >
      <el-table-column prop="workshopArchivesPo.numbering" label="车间编号" />
      <el-table-column prop="workshopArchivesPo.name" label="车间名称" />
      <el-table-column prop="productionLineNumber" label="产线编号" />
      <el-table-column prop="productionLineName" label="产线名称" />
      <el-table-column label="操作" align="center"  width="200" >
        <template slot-scope="scope">
           <el-link type="primary" :underline="false" class="upStr fz-1" @click="updateDetails(scope.row.id)">
            修改
          </el-link>
          <span class="line">|</span>
          <el-link type="danger" :underline="false" class="upStr redBtn" @click="submitDelete(scope.row.id)">
            删除
          </el-link>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      background
      :total="total"
      :page-sizes="[2, 4, 6, 8]"
      :page-size="paging.pageSize"
      :current-page="currentPage"
      layout="total, prev, pager, next, jumper"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <!-- 添加或修改 -->
    <el-dialog :title="dialogtit" :visible.sync="dialogFormVisibleAdd" width="30%">
      <el-form ref="pushData" :model="pushData" label-position="right" :rules="rules" class="addForm" label-width="80px" v-loading="formDataLoad">
        <el-form-item label="选择车间"  prop="workshopId">
          <el-select v-model="pushData.workshopId" >
            <el-option
              v-for="item in workshoplist"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产线名称"  prop="productionLineName">
          <el-input v-model="pushData.productionLineName" placeholder="请输入产品规格"  />
        </el-form-item>
        <div class="el-form-item flex">
          <div class="label" style="width:80px; text-align:right;padding-right:12px">产线产品</div>
          <div class="showlink" style="width:410px; text-align:left;">
            <div class="showlin-list w-100P" style="height: 150px; overflow:auto; ">
              <el-checkbox-group v-model="checkedids"  @change="CheckedChange">
                <el-checkbox v-for="proson in productoptions" :label="proson.id"  :key="proson.id">{{proson.productName}}</el-checkbox>
              </el-checkbox-group>
            </div>
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submit('pushData')">提交</el-button>
        <el-button @click="cancal">取消</el-button>
      </div>
    </el-dialog>
    <!-- 删除车间 -->
  </div>
</template>

<script>
import AddButton from '@/components/add-button/index.vue'
import {
  findProductionLinePageByParam,
  findProductionLineById,
  delProductionLineById,
  addProductionLine,
  updateProductionLine,
  findProductManagePageByParam,
  findWorkshopArchivesPageByParam
} from '@/child-business/api/IndustrialVision' 
export default {
  name: 'ProductType',
  components: { AddButton },
  data() {
    return {
      productoptions: [], // 产线产品的选项
      checkedids: [], // 已经选中的选项
      workshoplist: [],
      dialogtit: '新增',
      listLoading: true,
      showItem: true, // 是否展开
      currentPage: 1, // 默认当前页
      dialogFormVisibleAdd: false, // 新增弹窗
      formDataLoad: false,
      dialogVisible: false, // 提交
      paging: {
        pageNo: 1,
        pageSize: 12
      },
      queryBase: {
        numbering: '',
        name: ''
      },
      queryInfo: {
        productionLineName: '',
        productionLineNumber: ''
      },
      total: 0,
      tableData: [],
      pushData: {
        id: null,
        productManageIds: '',
        productionLineName: '',
        workshopId: ''
      },
      rules: {
        workshopId: [{ required: true, message: '请选择对应车间', trigger: 'change' }],
        productionLineName: [{ required: true, message: '请输入产线名称', trigger: 'change' }],
      },
    }
  },
  computed: {},
  created() {
    this.getList()
    this.getworkshoplist()
    this.getporductlist()
  },
  methods: {
    // 点击选择对应的产线产品
    CheckedChange(){
      this.pushData.productManageIds = this.checkedids.toString() //数组转换成字符串 
    },
    getporductlist(){
      const find = {
        pageNo: 1,
        pageSize: 10000,
        productManageVo: {
          productName: '',
          productTypeId: ''
        }
      }
      findProductManagePageByParam(find).then(response => {
        this.productoptions = response.records
      })
    },
    //获取车间列表
    getworkshoplist() {
      findWorkshopArchivesPageByParam({ workshopArchivesVO: { name: '', numbering: ''}, pageNo: 1, pageSize: 10000}).then(response => {
        this.workshoplist = response.records
      })
    },
    // 获取产品列表
    getList() {
      this.listLoading = true
      findProductionLinePageByParam({ productionLineVo: this.queryInfo, ...this.paging, ...this.queryBase}).then(response => {
        this.total = response.total
        this.tableData = response.records
        this.paging.pageNo = response.pageNo
        this.paging.pageSize = response.pageSize
        this.listLoading = false
      })
    },
    // 修改产品信息
    updateDetails(id) {
      this.dialogtit = '修改'
      this.dialogFormVisibleAdd = true
      this.formDataLoad = true
      findProductionLineById({ productionLineId: id}).then(response => {
        this.pushData.id = response.id
        this.pushData.workshopId = response.workshopId
        this.pushData.productManageIds = response.productManageIds
        this.checkedids = response.productManageIds.split(',')
        this.pushData.productionLineName = response.productionLineName
        this.formDataLoad = false
      })
    },
    // 提交添加/修改产品信息
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.dialogFormVisibleAdd = false
          if (this.pushData.id != null) {
            updateProductionLine(this.pushData).then(response => {
              if (response) {
                this.$message({ message: '修改成功！', type: 'success' })                
              }else{
                this.$message({ message: '修改失败！', type: 'error' })       
              }
              this.getList()
            })
          } else {
            delete this.pushData.id
            addProductionLine(this.pushData).then(response => {
              if (response) {
                this.$message({ message: '消息添加成功！', type: 'success'})
              }else{
                this.$message({ message: '修改失败！', type: 'error' })       
              }
              this.getList()
            })
          }
          this.checkedids = []
        } else {
          return false
        }
      })
    },
    // 取消提交添加产品信息
    cancal() {
      this.dialogFormVisibleAdd = false
    },
    handleSizeChange(val) {
      this.paging.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.paging.pageNo = val
      this.getList()
    },
    // 添加产品信息按钮
    handleAdd() {
      this.pushData = {
        id: null,
        productManageIds: '',
        productionLineName: '',
        workshopId: ''
      }
      this.dialogFormVisibleAdd = true
    },
    //重置
    reset() {
      this.paging.pageNo = 1
      this.queryInfo = {
        productionLineName: '',
        productionLineNumber: ''
      }
      this.queryBase = {
        numbering: '',
        name: ''
      }
      this.getList()
    },
    // 查询
    handleQuery() {
      this.paging.pageNo = 1
      this.getList()
    },
    // 删除提交
    submitDelete(id) {
      this.$confirm('此操作将永久删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delProductionLineById({ productionLineId: id }).then(response => {
          if (response) {
            this.$message({ message: '删除成功！', type: 'success' })
          } else {
            this.$message({ message: '删除失败！', type: 'error' })
          }
          this.getList()
        })
      }).catch(() => { })
    },
  }
}
</script>
<style lang="scss" scoped>
 @import '@/child-business/styles/IndustrialVision.scss';
 .el-checkbox{ 
   display: flex; justify-content: flex-start; width:33%;float:left; margin: vh(3px) 0; padding: 0 vw(7px);
  .el-checkbox__inner{margin-top:2px;}
 }
 .addForm{
  margin-left: 30px;
  .el-input{ width: 320px;}
  .el-select{ width: 320px;}
}
::v-deep .el-dialog__body{padding: 30px 20px 0}
</style>
